<?php

namespace app\index\controller;

use app\admin\model\Service;
use app\index\model\User;
use app\service\model\Chatlog;
use think\Controller;
use think\Session;

class Index extends Base
{
    /**
     * @return mixed
     */
    public function index()
    {

        $openid = $this->wechat_user['openid'];
        /**
         * @var User
         */
        $user = User::where('openid', $openid)->find();
        $service = Service::where('openid', $openid)->find();
        if ($service) {
            return $this->redirect('service/index/index');
        } elseif (!$user) {
            return $this->redirect('index/register/index');
        } elseif (1 != $user['status']) {
            return $this->redirect('index/register/ok');
        } else {
            !empty($this->wechat_user['nickname']) && $user->nickname = $this->wechat_user['nickname'];
            !empty($this->wechat_user['headimgurl']) && $user->headimgurl = $this->wechat_user['headimgurl'];
            $user->save();
        }
        $this->assign('user', $user);
        return $this->fetch();
    }

    // pc客户端
    /**
     * @return mixed
     */
    public function chat()
    {
        // 跳转到移动端
        if (request()->isMobile()) {
            $param = http_build_query([
                'id' => input('param.id'),
                'name' => input('param.name'),
                'group' => input('param.group'),
                'avatar' => input('param.avatar')
            ]);
            $this->redirect('/index/index/mobile?' . $param);
        }

        $this->assign([
            'socket' => config('socket'),
            'id' => input('param.id'),
            'name' => input('param.name'),
            'group' => input('param.group'),
            'avatar' => input('param.avatar')
        ]);

        return $this->fetch();
    }

    // 移动客户端
    /**
     * @return mixed
     */
    public function mobile()
    {
        $this->assign([
            'socket' => config('socket'),
            'id' => input('param.id'),
            'name' => input('param.name'),
            'group' => input('param.group'),
            'avatar' => input('param.avatar')
        ]);

        return $this->fetch();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function userChatLog($id)
    {
        $logs = Chatlog::where('user_id', $id)->order('id', 'desc')->select();
        $paylod = collect();
        $time = null;
        foreach ($logs->reverse() as $log) {
            $time_line = date('YmdHi', $log->time_line);
            if ($time_line != $time) {
                $time = $time_line;
                $paylod->push([
                    'time' => date('Y-m-d H:i:s', $log->time_line),
                    'type' => 'time'
                ]);
            }
            $paylod->push([
                'avatar' => $log->from_avatar,
                'content' => $log->content,
                'name' => $log->from_id == $id ? '我' : $log->from_name,
                'time' => date('Y-m-d H:i:s', $log->time_line),
                'type' => $log->from_id == $id ? 'mine' : 'other'
            ]);
        }
        return $paylod;
    }
}
